Hello Buddy, In this post we’ll get the knowledge about how to convert array data into Simple XML file using php code.
Most of times we want to get data as a XML format but we get data into array format. For this, we’ll go to convert the array data into Simple XML file. During this tutorial we’ll discuss, the way to produce XML from array in php. We have to create an easy script to convert Any PHP array data file into XML file. You’ll simply generate XML file from PHP array ans save this into XML file.You’ll convert every kind of array like Associative array or 3-d arrays.
Step-1:
In the First step, we ill store the product data into a php variable ($products_array)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$products_array = array( 0 => array('adidas Workout Shorts','USD 24.99','Fan Shop','ADIDAS','Dkgreyene'), 1 => array('adidas Boys Influencer Shorts','USD 23.99','Outdoor Recreation','adidas','Gold'), 2 => array('Shoes','USD 69.98','Boys','Adidas','WHITE'), 3 => array('adidas Originals Icon Backpack','USD 55.00','Clothing Accessories','ADIDAS','Black'), 4 => array('adidas Originals Icon Backpack','USD 55.00','Clothing Accessories','ADIDAS','Red'), 5 => array('ADIDAS Top','USD 56.00','Active Shirts','ADIDAS','Sky blue'), 6 => array('adidas Originals Spezial Sneaker','USD 52.99','Footwear','adidas','BLUE'), 7 => array('ADIDAS Shorts','USD 59.00','Clothing','ADIDAS','Black'), 8 => array('adidas Originals Black Sneakers','USD 63.00','Boys','Adidas','Black'), 9 => array('ADIDAS ORIGINALS Sneakers','USD 53.00','Athletic Outdoor','ADIDAS ORIGINALS','White') ); |
Step-2:
Now we need to create a simple script to convert products array data into XML file. PHP script which is used to convert array data into xml is given below:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//create a new xml object $xml = new SimpleXMLElement('<product/>'); //loop through the data, and add each record to the xml object foreach($products AS $productDetails){ $product = $xml->addChild('product'); $product->addChild('ProductName', $productDetails[0]); $product->addChild('Price', $productDetails[1]); $product->addChild('Category', $productDetails[2]); $product->addChild('Brand', $productDetails[3]); $product->addChild('Color', $productDetails[4]); } //save the xml file $xml->asXML("products.xml"); |
Step-3:
Now you will get the “products.xml” file into same folder, in which folder you php script is saved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
<?xml version="1.0"?> <product> <product> <ProductName>adidas Workout Shorts</ProductName> <Price>USD 24.99</Price> <Category>Fan Shop</Category> <Brand>ADIDAS</Brand> <Color>Dkgreyene</Color> </product> <product> <ProductName>adidas Boys Influencer Shorts</ProductName> <Price>USD 23.99</Price> <Category>Outdoor Recreation</Category> <Brand>adidas</Brand> <Color>Gold</Color> </product> <product> <ProductName>Shoes</ProductName> <Price>USD 69.98</Price> <Category>Boys</Category> <Brand>Adidas</Brand> <Color>WHITE</Color> </product> <product> <ProductName>adidas Originals Icon Backpack</ProductName> <Price>USD 55.00</Price> <Category>Clothing Accessories</Category> <Brand>ADIDAS</Brand> <Color>Black</Color> </product> <product> <ProductName>adidas Originals Icon Backpack</ProductName> <Price>USD 55.00</Price> <Category>Clothing Accessories</Category> <Brand>ADIDAS</Brand> <Color>Red</Color> </product> <product> <ProductName>ADIDAS Top</ProductName> <Price>USD 56.00</Price> <Category>Active Shirts</Category> <Brand>ADIDAS</Brand> <Color>Sky blue</Color> </product> <product> <ProductName>adidas Originals Spezial Sneaker</ProductName> <Price>USD 52.99</Price> <Category>Footwear</Category> <Brand>adidas</Brand> <Color>BLUE</Color> </product> <product> <ProductName>ADIDAS Shorts</ProductName> <Price>USD 59.00</Price> <Category>Clothing</Category> <Brand>ADIDAS</Brand> <Color>Black</Color> </product> <product> <ProductName>adidas Originals Black Sneakers</ProductName> <Price>USD 63.00</Price> <Category>Boys</Category> <Brand>Adidas</Brand> <Color>Black</Color> </product> <product> <ProductName>ADIDAS ORIGINALS Sneakers</ProductName> <Price>USD 53.00</Price> <Category>Athletic Outdoor</Category> <Brand>ADIDAS ORIGINALS</Brand> <Color>White</Color> </product> </product> |
Finally, our step by step process to convert array data into XML file is completed.
You’ll get complete code ( step-1 to step-3 ) from below:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?php $products = array( 0 => array('adidas Workout Shorts','USD 24.99','Fan Shop','ADIDAS','Dkgreyene'), 1 => array('adidas Boys Influencer Shorts','USD 23.99','Outdoor Recreation','adidas','Gold'), 2 => array('Shoes','USD 69.98','Boys','Adidas','WHITE'), 3 => array('adidas Originals Icon Backpack','USD 55.00','Clothing Accessories','ADIDAS','Black'), 4 => array('adidas Originals Icon Backpack','USD 55.00','Clothing Accessories','ADIDAS','Red'), 5 => array('ADIDAS Top','USD 56.00','Active Shirts','ADIDAS','Sky blue'), 6 => array('adidas Originals Spezial Sneaker','USD 52.99','Footwear','adidas','BLUE'), 7 => array('ADIDAS Shorts','USD 59.00','Clothing','ADIDAS','Black'), 8 => array('adidas Originals Black Sneakers','USD 63.00','Boys','Adidas','Black'), 9 => array('ADIDAS ORIGINALS Sneakers','USD 53.00','Athletic Outdoor','ADIDAS ORIGINALS','White') ); //create a new xml object $xml = new SimpleXMLElement('<product/>'); //loop through the data, and add each record to the xml object foreach($products AS $productDetails){ $product = $xml->addChild('product'); $product->addChild('ProductName', $productDetails[0]); $product->addChild('Price', $productDetails[1]); $product->addChild('Category', $productDetails[2]); $product->addChild('Brand', $productDetails[3]); $product->addChild('Color', $productDetails[4]); } //save the xml file $xml->asXML("products.xml"); ?> |
Are you want to get implementation help, or modify or extend the functionality of this script?
Leave your comment at below comment box.
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co